﻿using System.IO;
using Microsoft.SharePoint;

namespace AgileDirect.SPDToolkit.Workflows.Foundation
{
    /// <summary>
    /// This feature receiver is reponsible for adding and associating workflows
    /// `</summary>
    public class SPDWorkflowReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// On activating add the workflows to the site
        /// </summary>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var web = properties.Feature.Parent as SPWeb;
            if (web != null)
            {
                string rootDirectory = Path.Combine(properties.Definition.
                    RootDirectory, "Workflows");

                string[] workflows = Directory.GetDirectories(rootDirectory);
                foreach (string directory in workflows)
                {
                    string workflow = Path.GetFileName(directory);
                    SPDWorkflowManager.ImportWorkflowInformation(workflow, web.Url, directory + "\\");
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //Not implemented
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //Not implemented
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //Not implemented
        }
    }
}